﻿using XAMLStylePad.Models;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using FireFly;
using System.Collections;

namespace XAMLStylePad.ViewModels
{
    public class SourceViewModel: XamlViewModelBase<SourceBase>
    {
        #region .ctor
        public SourceViewModel(SourceBase src)
            : base(src)
        {
        } 
        #endregion

        // properties

        #region Name
        public string Name
        {
            get
            {
                return Model.Name;
            }
            set
            {
                Model.Name = value;
                OnPropertyChanged("Name");
            }
        }
        #endregion

        #region FullPath
        public string FullPath
        {
            get
            {
                return Model.FullPath;
            }
            set
            {
                Model.FullPath = value;
                OnPropertyChanged("FullPath");
            }
        }
        #endregion

        #region Directory
        public string Directory
        {
            get
            {
                return Model.Directory;
            }
            set
            {
                Model.Directory = value;
                OnPropertyChanged("Directory");
            }
        }
        #endregion

        #region IsLoaded
        public bool IsLoaded
        {
            get
            {
                return Model.IsLoaded;
            }
        }
        #endregion

        #region IsReadOnly
        public override bool IsReadOnly
        {
            get
            {
                return Model.IsReadOnly;
            }
        }
        #endregion

        #region Resources
        public CollectionViewModel<ResourceDictionaryModel, ResourceDictionaryViewModel> Resources
        {
            get
            {
                if(_Resources == null)
                {
                    if(!Model.IsLoaded)
                        Model.Load();

                    _Resources = new CollectionViewModel<ResourceDictionaryModel, ResourceDictionaryViewModel>(Model.Resources, o => new ResourceDictionaryViewModel(o))
                    {
                        DisplayName = "Resources",
                        Parent = this
                    };
                }
                return _Resources;
            }
        }
        CollectionViewModel<ResourceDictionaryModel, ResourceDictionaryViewModel> _Resources;
        #endregion

        #region overrided
        public override string DisplayName
        {
            get
            {
                return Name ?? "New XAML file";
            }
        }

        public override IEnumerable<IRelayCommand> Commands
        {
            get
            {
                return new IRelayCommand[] 
                { 
                    SaveCommand,
                    SaveAsCommand,
                    RemoveCommand
                };
            }
        }
        #endregion

        // commands

        #region SaveCommand
        public static IRelayCommand SaveCommand { get { return _SaveCommand; } }
        static RelayCommand<SourceViewModel> _SaveCommand = new EditCommand<SourceViewModel>(context =>
        {
            var vm = context.ViewModel;
            if(!vm.IsReadOnly)
            {
                if(vm.FullPath != null)
                    vm.Model.Save(context.ViewModel.Model.FullPath);
                else
                    SaveAsCommand.Execute(context);
            }
        }, 
        "SourceViewModel.SaveCommand");
        #endregion

        #region SaveAsCommand
        public static IRelayCommand SaveAsCommand { get { return _SaveAsCommand; } }
        static RelayCommand<SourceViewModel> _SaveAsCommand = new EditCommand<SourceViewModel>(context =>
        {
            SaveFileDialog dlg = new SaveFileDialog()
            {
                InitialDirectory = Environment.CurrentDirectory,
                OverwritePrompt = true,
                Filter = "Assemblies and executes(*.dll, *.exe)|*.dll;*.exe",
            };
            dlg.OverwritePrompt = true;
            if(dlg.ShowDialog() == true)
            {
                context.ViewModel.Model.FullPath = dlg.FileName;
                context.ViewModel.Model.Save(dlg.FileName);
            }
        },
        "SourceViewModel.SaveAsCommand");
        #endregion

        //#region RemoveSourceCommand
        //public static RelayCommand<SourceViewModel> RemoveSourceCommand
        //{
        //    get
        //    {
        //        if(_RemoveSourceCommand == null)
        //        {
        //            _RemoveSourceCommand = new EditingRelayCommand<SourceViewModel>(
        //                context =>
        //                {
        //                    var vm = context.ViewModels;
        //                    ((ProjectViewModel)vm.Parent).Sources.Remove(vm);
        //                },
        //                "SourceViewModel.RemoveSourceCommand"
        //            );
        //        }
        //        return _RemoveSourceCommand;
        //    }
        //}
        //static RelayCommand<SourceViewModel> _RemoveSourceCommand;
        //#endregion

        public override ICollection VisualChildren
        {
            get
            {
                return Resources;
            }
        }
    }
}